package com.chuzhiyun.chengguoxy.feature.home;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.chuzhiyun.chengguoxy.R;

import android.Manifest;
import android.view.View;
import com.chuzhiyun.chengguoxy.databinding.ActivityHomeBinding;
import com.chuzhiyun.chengguoxy.feature.base.BaseActivity;
import com.chuzhiyun.chengguoxy.feature.bean.UserInfoBean;
import com.chuzhiyun.chengguoxy.feature.internship.InternshipFragment;
import com.chuzhiyun.chengguoxy.feature.message.MessageFragment;
import com.chuzhiyun.chengguoxy.feature.mine.MineFragment;
import com.chuzhiyun.chengguoxy.feature.position.PositionFragment;
import com.chuzhiyun.chengguoxy.global.UserConfig;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.google.gson.Gson;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.winton.bottomnavigationview.NavigationView;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("FieldCanBeLocal")
public class HomeActivity extends BaseActivity<ActivityHomeBinding> {
    private List<Fragment> fragments;// used for ViewPager adapter
    private VpAdapter mAdapter;
    @Override
    public void initView(View view) {

        fragments = new ArrayList<>(4);
        InternshipFragment fragment0 = new InternshipFragment();
        PositionFragment fragment1 = new PositionFragment();
        MessageFragment fragment2 = new MessageFragment();
        MineFragment fragment3 = new MineFragment();
        fragments.add(fragment0);
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);

        List<NavigationView.Model> tabs = new ArrayList<>();
        tabs.add(new NavigationView.Model.Builder(R.mipmap.position_select,R.mipmap.position_normal).title("职位").build());
        tabs.add(new NavigationView.Model.Builder(R.mipmap.work_select,R.mipmap.work_normal).title("工作台").build());
        tabs.add(new NavigationView.Model.Builder(R.mipmap.message_select,R.mipmap.message_normal).title("消息").build());
        tabs.add(new NavigationView.Model.Builder(R.mipmap.mine_select,R.mipmap.mine_normal).title("我的").build());
        viewBinding.nv.setItems(tabs);
        viewBinding.nv.setOnTabSelectedListener(new NavigationView.OnTabSelectedListener() {
            @Override
            public void selected(int i, NavigationView.Model model) {
                viewBinding.vp.setCurrentItem(i);
            }

            @Override
            public void unselected(int i, NavigationView.Model model) {

            }
        });
        
        viewBinding.nv.build();
        viewBinding.nv.reminder(0,false,"");
        viewBinding.nv.reminder(1,false,"");
        viewBinding.nv.reminder(2,false,"");
        viewBinding.nv.reminder(3,false,"");

        mAdapter = new VpAdapter(getSupportFragmentManager(), fragments);
        viewBinding.vp.setAdapter(mAdapter);
        viewBinding.vp.setOffscreenPageLimit(3);
        viewBinding.vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewBinding.nv.check(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        initPermissions();
    }

    private static class VpAdapter extends FragmentPagerAdapter {
        private final List<Fragment> data;
        @SuppressWarnings("deprecation")
        VpAdapter(FragmentManager fm, List<Fragment> data) {
            super(fm);
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @NotNull
        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }


    }

    private void initPermissions() {

        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_SETTINGS).subscribe(new Observer<Boolean>() {
            @Override
            public void onSubscribe(@NotNull Disposable d) {

            }

            @Override
            public void onNext(@NotNull Boolean aBoolean) {

            }

            @Override
            public void onError(@NotNull Throwable e) {

            }

            @Override
            public void onComplete() {
                //homefragment.GPS();
            }
        });
    }

    @Override
    public void initData() {
        getUserInfo();
    }

    private void getUserInfo(){
        String url = NetApi.getStudentInfo();
        NetService.getInstance().get(url, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
                if (statusCode==200){
                    Gson gson = new Gson();
                    UserInfoBean bean = gson.fromJson(response,UserInfoBean.class);
                    UserConfig.setInstance(bean);
                    String studentId = UserConfig.getInstance().getId().toString();
                    System.out.println(studentId);
                }
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
            }
        });
    }

    @Override
    public void setListener() {

    }

    @Override
    protected ActivityHomeBinding getViewBinding() {
        return ActivityHomeBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }
}